<?php
/**
 * +--------------------------------------------------------+
 * @Created by PhpStorm.
 * @Category ArticleController.php
 * @Depiction: 文章管理
 * Author: hwy
 * Date: 2020/9/17  16:24
 * +--------------------------------------------------------+
 */

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Http\Requests\Article\Store;
use App\Models\Article;
use App\Models\Category;
use App\Services\ArticleService;
use App\Services\CategoryService;
use App\Services\TagService;
use Illuminate\Contracts\View\Factory;
use Illuminate\Http\Request;
use Illuminate\View\View;

class ArticleController extends Controller
{
    /**
     * @var string
     */
    protected $_model = 'article';

    /**
     * 文章服务类
     * @var ArticleService
     */
    protected $articleService;

    /**
     * @var 栏目服务类
     */
    protected $categoryService;

    /**
     * 文章标签服务类
     * @var TagService
     */
    protected $tagService;

    /**
     * TagController constructor.
     */
    public function __construct()
    {
        $this->articleService  = ArticleService::getInstance();
        $this->tagService      = TagService::getInstance();
        $this->categoryService = CategoryService::getInstance();
    }

    /**
     * @param Request $request
     * @return Factory|View
     * @Depiction
     * @Author hwy
     * @Date  2021/2/22  17:44
     */
    public function index(Request $request)
    {
        $compact = compact('request');

        return view('admin.' . $this->_model . '.index', $compact);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @Depiction
     * @Author hwy
     * @Date  2021/2/23  14:00
     */
    public function getList(Request $request)
    {
        $responseData = $this->articleService->getList($request);

        return response()->json($responseData);
    }

    /**
     * @param Request $request
     * @return Factory|View
     * @Depiction
     * @Author hwy
     * @Date  2021/2/23  14:00
     */
    public function create(Request $request)
    {
        $category = $this->categoryService->getCateLevel();
        $tag      = $this->tagService->getAll();
        $compact  = compact('request', 'category', 'tag');

        return view('admin.' . $this->_model . '.create', $compact);
    }

    /**
     * @param Store $request
     * @return \Illuminate\Http\JsonResponse
     * @Depiction
     * @Author hwy
     * @Date  2021/2/23  14:00
     */
    public function store(Store $request)
    {
        $responseData = $this->articleService->addData($request);

        return response()->json($responseData);

    }

    /**
     * @param Request $request
     * @param $id
     * @return Factory|View
     * @Depiction
     * @Author hwy
     * @Date  2020/10/9  20:36
     */
    public function edit(Request $request, $id)
    {
        $data     = $this->articleService->getOneWith($id, ['has_one_image', 'tags']);
        $category = $this->categoryService->getCateLevel();
        $tag      = $this->tagService->getAll();
        foreach ($tag as $value) {
            $value->checked = $data->tags->contains($value) ? 'checked' : '';
        }
        $compact = compact('request', 'data', 'category', 'tag');

        return view('admin.' . $this->_model . '.edit', $compact);
    }

    /**
     * @param Request $request
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     * @Depiction
     * @Author hwy
     * @Date  2020/9/17  16:17
     */
    public function update(Store $request, $id)
    {
        $responseData = $this->articleService->editData($request, $id);

        return response()->json($responseData);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @Depiction
     * @Author hwy
     * @Date 2021/5/27  15:48
     */
    public function destroy(Request $request)
    {
        $ids          = $request->id;
        $responseData = $this->articleService->delData($ids);

        return response()->json($responseData);
    }


}
